package com.boliq.demo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.boliq.demo.R;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView userHead;
        private TextView userName;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            userHead = itemView.findViewById(R.id.tv_user_head);
            userName = itemView.findViewById(R.id.tv_user_name);
        }
        public void setUserName(String name){
            userName.setText(name);
        }
    }

    private List<String> data;
    public MyAdapter(){
        data = new ArrayList<>();
        //这一步是模拟一些数据
        for (int i = 0; i < 20; i++) { data.add("用户"+i); }
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater
                .from(parent.getContext())
                .inflate(R.layout.item_user_info,
                        parent,
                        false);//一定要加这个参数false
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String name = data.get(position);
        holder.userName.setText(name);
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    public void addItem(String item){
        data.add(item);
        notifyItemChanged(data.size()-1);
    }

}
